package edu.ucsd.cse135.gas.action.applicant;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.ucsd.cse135.gas.bean.applicant.UniversityBean;
import edu.ucsd.cse135.gas.logic.Degree;
import edu.ucsd.cse135.gas.logic.support.Discipline;
import edu.ucsd.cse135.gas.logic.support.University;
import edu.ucsd.cse135.gas.resources.Constants;

public class ChooseUniversityAction extends Action {

	public ChooseUniversityAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
	
		UniversityBean universityBean = (UniversityBean)form;
		
		Degree degree = (Degree)request.getSession().getAttribute("degree");
		
		if(universityBean.getId() == 0)
		{
			degree.setUniversityId(University.storeCustomUniversity(universityBean.getName(), degree.getLocationId()));
			degree.setUniversity(universityBean.getName());
		}
		else
		{
			degree.setUniversityId(universityBean.getId());
			degree.setUniversity(universityBean.getName());
		}
		
		request.getSession().setAttribute("degree", degree);
		
		/** Get disciplines **/	
		
		ArrayList<Discipline> disciplineList = Discipline.getDisciplines();
		ArrayList<HashMap<String, String>> disciplines = new ArrayList<HashMap<String, String>>();
	
		/*** Iterate over all disciplines and display them in a radio button menu ***/
		
		for(int i=0; i < disciplineList.size(); i++)
		{
			HashMap<String, String> discipline = new HashMap<String, String>();
			
			discipline.put("id", String.valueOf(disciplineList.get(i).getId()));
			discipline.put("name", disciplineList.get(i).getName());
			
			disciplines.add(discipline);
		}
		
		request.setAttribute("disciplines", disciplines);
		
		return mapping.findForward(Constants.SUCCESS);
		
	}
}